package com.gengine.utils
{
	import flash.events.*;
	import flash.net.*;
	import flash.utils.*;
	
	public class FilterText extends EventDispatcher
	{
		private const MAX_LENGTH:int = 1000;
		private var _loader:URLLoader;
		private var _filterStr:String;
		private var _filterRegExp:RegExp;
		private var _splitReg:RegExp;
		private var _wordMap:Dictionary;
		private var _filterRegAry:Array;
		private static var _instance:FilterText;
		
		public function FilterText()
		{
			this._splitReg = /(\n|\r)+""(\n|\r)+/mg;
			this._filterRegAry = [];
			this._loader = new URLLoader();
			this._wordMap = new Dictionary();
			
		}
		
		public function load(param1:String) : void
		{
			var file:* = param1;
			var request:* = new URLRequest(file);
			try
			{
				this._loader.load(request);
				this.addListener();
			}
			catch (error:Error)
			{
				trace("Unable to load requested document.");
			}
			
		}
		
		private function addListener() : void
		{
			this._loader.addEventListener(Event.COMPLETE, this.completeHandler);
			this._loader.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
			
		}
		
		private function removelistener() : void
		{
			this._loader.removeEventListener(Event.COMPLETE, this.completeHandler);
			this._loader.removeEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
			
		}
		
		private function completeHandler(event:Event) : void
		{
			this.log("加载完成");
			this.removelistener();
			var _loc_2:* = URLLoader(event.target);
			this.createRegExpStr(String(_loc_2.data));
			dispatchEvent(event);
			
		}
		
		private function createRegExpStr(param1:String) : void
		{
			var _loc_2:* = param1.split(this._splitReg);
			var _loc_3:* = _loc_2.length;
			var _loc_4:int = 0;
			while (_loc_4 < _loc_3)
			{
				
				this.addWord(_loc_2[_loc_4]);
				_loc_4++;
			}
			
		}
		
		public function ioErrorHandler(event:IOErrorEvent) : void
		{
			trace(event);
			dispatchEvent(event);
			
		}
		
		public function getFilterStr(param1:String) : String
		{
			var _loc_3:String = null;
			var _loc_4:* = undefined;
			if (param1 == null || param1 == "")
			{
				return "";
			}
			var _loc_2:* = param1.length;
			var _loc_5:int = 0;
			while (_loc_5 < _loc_2)
			{
				
				_loc_3 = param1.charAt(_loc_5);
				if (_loc_3 != "*")
				{
					_loc_4 = this._wordMap[_loc_3];
					if (_loc_4 is String)
					{
						var _loc_6:* = new RegExp("(" + _loc_4 + ")", "img");
						this._wordMap[_loc_3] = new RegExp("(" + _loc_4 + ")", "img");
						_loc_4 = _loc_6;
					}
					param1 = param1.replace(_loc_4, this.regHandler);
				}
				_loc_5++;
			}
			return param1;
		}
		
		private function regHandler() : String
		{
			arguments = arguments[1].toString();
			return arguments.replace(/.{1}"".{1}/g, "*");
		}
		
		private function log(param1:Object) : void
		{
			trace(param1);
			
		}
		
		public function setFilterStr(param1:String, param2:RegExp = null) : void
		{
			if (param2 != null)
			{
				this._splitReg = param2;
			}
			this.createRegExpStr(param1);
			
		}
		
		public function addWord(param1:String) : void
		{
			var _loc_2:String = null;
			var _loc_3:String = null;
			param1 = param1.replace("\n", "");
			if (param1 != null && param1.length > 0)
			{
				_loc_3 = param1.charAt(0);
				_loc_2 = this._wordMap[_loc_3] as String;
				if (_loc_2)
				{
					this._wordMap[_loc_3] = this._wordMap[_loc_3] + ("|" + param1);
				}
				else
				{
					this._wordMap[_loc_3] = param1;
				}
			}
			
		}
		
		public static function get instance() : FilterText
		{
			if (_instance == null)
			{
				_instance = new FilterText;
			}
			return _instance;
		}
		
	}
}
